package cn.cvs.service.impl;

import cn.cvs.dao.SysUserMapper;
import cn.cvs.pojo.Supplier;
import cn.cvs.pojo.SysUser;
import cn.cvs.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Scope("prototype")
@Transactional
@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Override
    public List<SysUser> getList(SysUser sysUser) {
        return sysUserMapper.selectSysUserList(sysUser);
    }

    @Override
    public boolean add(SysUser sysUser) {
        boolean result = false;
        try {
            if (sysUserMapper.add(sysUser) == 1){
                result = true;
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    @Override
    public boolean Testadd(Supplier supplier) {
        boolean result = false;
        try {
            if (sysUserMapper.Testadd(supplier) == 1) {
                System.out.println("添加成功，但没提交事务");
                result = true;
                if (result == false) {
                    System.out.println("异常，回滚事务");
                    throw new RuntimeException();
                }
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    @Override
    public boolean del(int id) {
        boolean result = false;
        try {
            if (sysUserMapper.die(id) == 1){
                System.out.println("删除成功！");
                result = true;
                if (result == false){
                    System.out.println("异常，回滚事务");
                    throw new RuntimeException();
                }
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }

//    public SysUserMapper getSysUserMapper() {
//        return sysUserMapper;
//    }
//
//    public void setSysUserMapper(SysUserMapper sysUserMapper) {
//        this.sysUserMapper = sysUserMapper;
//    }
}
